﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DingTalkAgent.DBUtility;
namespace DingTalkAgent.SqlData
{
    //字典条目
    public partial class Dictionary
    {
        #region	 T_Dictionary

        ///<summary>
        /// Exist
        ///</summary>
        public bool Exists(int Dic_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Dictionary");
            strSql.Append(" where ");
            strSql.Append(" Dic_ID = @Dic_ID  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Dic_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = Dic_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条T_Dictionary数据
        /// </summary>
        public int Add(DingTalkAgent.Model.Dictionary model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Dictionary(");
            strSql.Append("Code,Parent_ID,Name,OrderID,Active,Remark");
            strSql.Append(") values (");
            strSql.Append("@Code,@Parent_ID,@Name,@OrderID,@Active,@Remark");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@Code", SqlDbType.VarChar,24) ,
                        new SqlParameter("@Parent_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Name", SqlDbType.VarChar,64) ,
                        new SqlParameter("@OrderID", SqlDbType.Int,4) ,
                        new SqlParameter("@Active", SqlDbType.Bit,1) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,512)

            };

            parameters[0].Value = model.Code;
            parameters[1].Value = model.Parent_ID;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.OrderID;
            parameters[4].Value = model.Active;
            parameters[5].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条T_Dictionary数据
        /// </summary>
        public bool Update(DingTalkAgent.Model.Dictionary model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Dictionary set ");

            strSql.Append(" Code = @Code , ");
            strSql.Append(" Parent_ID = @Parent_ID , ");
            strSql.Append(" Name = @Name , ");
            strSql.Append(" OrderID = @OrderID , ");
            strSql.Append(" Active = @Active , ");
            strSql.Append(" Remark = @Remark  ");
            strSql.Append(" where Dic_ID=@Dic_ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@Dic_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Code", SqlDbType.VarChar,24) ,
                        new SqlParameter("@Parent_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Name", SqlDbType.VarChar,64) ,
                        new SqlParameter("@OrderID", SqlDbType.Int,4) ,
                        new SqlParameter("@Active", SqlDbType.Bit,1) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,512)

            };

            parameters[0].Value = model.Dic_ID;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Parent_ID;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.OrderID;
            parameters[5].Value = model.Active;
            parameters[6].Value = model.Remark;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条T_Dictionary数据
        /// </summary>
        public bool Delete(int Dic_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Dictionary ");
            strSql.Append(" where Dic_ID=@Dic_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Dic_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = Dic_ID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条T_Dictionary数据
        /// </summary>
        public bool Deletes(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Dictionary ");
            strSql.Append(" where ID in (" + ids + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DingTalkAgent.Model.Dictionary GetModel(int Dic_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Dic_ID, Code, Parent_ID, Name, OrderID, Active, Remark  ");
            strSql.Append("  from T_Dictionary ");
            strSql.Append(" where Dic_ID=@Dic_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Dic_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = Dic_ID;


            DingTalkAgent.Model.Dictionary model = new DingTalkAgent.Model.Dictionary();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Dic_ID"].ToString() != "")
                {
                    model.Dic_ID = int.Parse(ds.Tables[0].Rows[0]["Dic_ID"].ToString());
                }
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                if (ds.Tables[0].Rows[0]["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(ds.Tables[0].Rows[0]["Parent_ID"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                if (ds.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Active"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Active"].ToString() == "1") || (ds.Tables[0].Rows[0]["Active"].ToString().ToLower() == "true"))
                    {
                        model.Active = true;
                    }
                    else
                    {
                        model.Active = false;
                    }
                }
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据T_Dictionary列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Dictionary ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据T_Dictionary列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.Dictionary> IEnumGetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Dic_ID, Code, Parent_ID, Name, OrderID, Active, Remark ");
            strSql.Append(" FROM T_Dictionary ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.Dictionary model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.Dictionary();
                if (dr["Dic_ID"].ToString() != "")
                {
                    model.Dic_ID = int.Parse(dr["Dic_ID"].ToString());
                    model.id = model.Dic_ID;
                }
                model.Code = dr["Code"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                model.text = model.Name;
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }
                if (dr["Active"].ToString() != "")
                {
                    if ((dr["Active"].ToString() == "1") || (dr["Active"].ToString().ToLower() == "true"))
                    {
                        model.Active = true;
                    }
                    else
                    {
                        model.Active = false;
                    }
                }
                model.Remark = dr["Remark"].ToString();

                yield return model;
            }
        }
        /// <summary>
        /// 获得数据 T_Dictionary分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="total">总记录数</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public List<DingTalkAgent.Model.Dictionary> GetPageList(int PageIndex, int PageSize, out int total,int Parent_ID, string keyword)
        {
            int Skip = (PageIndex - 1) * PageSize;
            //count
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select @total=count(Dic_ID) From T_Dictionary ");
            //data
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " * from (");
            strSql.Append("select  row_number() OVER (ORDER BY Dic_ID Desc) AS row, Dic_ID, Code, Parent_ID, Name, OrderID, Active, Remark ");
            strSql.Append(" FROM  T_Dictionary ");
            //where
            StringBuilder Where = new StringBuilder();
            Where.Append(" where 1=1");
            if (keyword.Trim() != "")
            {
                Where.Append(" and (Name like @keyword or Remark like @keyword) ");
            }
            if (Parent_ID > 0)
            {
                Where.Append(" and Parent_ID="+Parent_ID);
            }

            strSqlCount.Append(Where.ToString());
            strSql.Insert(0, strSqlCount.ToString() + ";");
            strSql.Append(Where.ToString());
            strSql.Append(") as a ");
            strSql.Append(" Where a.row>" + Skip);

            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@total", SqlDbType.Int,4),
                    new SqlParameter("@keyword", SqlDbType.VarChar,64)
                    };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = "%" + keyword.Trim() + "%";

            List<DingTalkAgent.Model.Dictionary> list = new List<DingTalkAgent.Model.Dictionary>();
            DingTalkAgent.Model.Dictionary model = new DingTalkAgent.Model.Dictionary();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            total = Convert.ToInt32(parameters[0].Value);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.Dictionary();
                if (dr["Dic_ID"].ToString() != "")
                {
                    model.Dic_ID = int.Parse(dr["Dic_ID"].ToString());
                    model.id = model.Dic_ID;
                }
                model.Code = dr["Code"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                model.text = model.Name;
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }
                if (dr["Active"].ToString() != "")
                {
                    if ((dr["Active"].ToString() == "1") || (dr["Active"].ToString().ToLower() == "true"))
                    {
                        model.Active = true;
                    }
                    else
                    {
                        model.Active = false;
                    }
                }
                model.Remark = dr["Remark"].ToString();

                list.Add(model);
            }
            return list;
        }
        #endregion
    }
}
